package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.operation.GetItemInfoRequest;
import com.wg.dome.operation.GetItemInfoResponse;
import com.wg.dome.repository.EquipRepository;
import com.wg.dome.repository.HeroRepository;
import com.wg.dome.repository.ItemRepository;
import com.wg.core.net.OperationRequest;
import com.wg.core.net.OperationResponse;
import com.wg.core.net.SendParameters;

/**
 * 获取玩家物品信息
 */
public class HandleRequest4 extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        GetItemInfoRequest getItemInfoRequest = new GetItemInfoRequest(request);
        if (!getItemInfoRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(getItemInfoRequest);
        }

        if (getItemInfoRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, getItemInfoRequest.getPlayerId(), peer.getPlayerId());
        }

        ItemRepository itemRepository = new ItemRepository();
        EquipRepository equipRepository = new EquipRepository();
        HeroRepository heroRepository = new HeroRepository();

        GetItemInfoResponse getPackInfoResponse = new GetItemInfoResponse();
        getPackInfoResponse.setItems(itemRepository.toHashTables(peer.getPlayerId()));
        getPackInfoResponse.setEquips(equipRepository.toHashTables(peer.getPlayerId()));
        getPackInfoResponse.setHeros(heroRepository.toHashTables(peer.getPlayerId()));

        return new OperationResponse(request.getOperationCode(), getPackInfoResponse);
    }
}
